let testData = `
{
    "AListName": "A",
    "AClassList": [
        {
            "AClassName": "Sleep",
            "AClassImg": "https://s21.ax1x.com/2024/04/10/pFXPTeK.png",
            "ClassInfList": [
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "March 27, 2024",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                }
            ]
        },
        {
            "AClassName": "Clean",
            "AClassImg": "https://s21.ax1x.com/2024/04/10/pFXi7Xq.png",
            "ClassInfList": [
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                }
            ]
        },
        {
            "AClassName": "Work",
            "AClassImg": "https://s21.ax1x.com/2024/04/10/pFXiXAU.png",
            "ClassInfList": [
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                }
            ]
        },
        {
            "AClassName": "Diet",
            "AClassImg": "https://s21.ax1x.com/2024/04/10/pFXivh4.png",
            "ClassInfList": [
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                }
            ]
        },
        {
            "AClassName": "Sport",
            "AClassImg": "https://s21.ax1x.com/2024/04/10/pFXiz9J.png",
            "ClassInfList": [
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                },
                {
                    "Link": "https://www.bilibili.com",
                    "Img": "https://s21.ax1x.com/2024/04/10/pFXP2i4.png",
                    "Time": "New",
                    "Title": "Waiting for the wind day",
                    "From": "Benjamin Booker"
                }
            ]
        }
    ]
}
`

async function getMessage(defaultData, listName) {
    defaultData = JSON.parse(defaultData);
    if(!listName) {
        return defaultData;
    }

    // 创建 JSON 数据
    let messageData = {
        "AListName": listName,
        "AClassList": [{}],
    };

    let classList = null;
    // 获取类数据
    try {
        let response = await fetch("http://localhost:5050/message/getMessageTypeClassList", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
        });
        // 转换为 json 数据
        let responseJSON = await response.json();
        // 判断是否成功
        if (responseJSON.isSuccess === false) {
            console.log("Request Class Failed: ", responseJSON.msg);
            return defaultData;
        }

        // 获取列表
        classList = await responseJSON.data;
    } catch (e) {
        console.log("Request Class Failed: ", e);
        return defaultData;
    }

    // 赋值
    messageData.AClassList = []
    classList.forEach(theClass => {
        messageData.AClassList.push({
            "AClassName": theClass.name,
            "AClassImg": theClass.img,
            "ClassInfList": [{}],
        });
    });

    // 遍历类获取数据块
    for(let i = 0; i < classList.length; i++) {
        let messageList = null;
        // 尝试获取
        try {
            let requestJSON = {"id": classList[i].ID};
            let response = await fetch("http://localhost:5050/message/getClassMessageBlockList", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify(requestJSON),
            });
            // 转换
            let responseJSON = await response.json();
            // 判断
            if (responseJSON.isSuccess === false) {
                console.log("Request Message Failed: ", requestJSON.msg);
                return defaultData;
            }

            // 获取信息块列表
            messageList = await responseJSON.data;
        } catch (e) {
            console.log("Request Message Failed: ", e)
            return defaultData;
        }

        messageData.AClassList[i].ClassInfList = [];
        // 赋值
        messageList.forEach((message) => {
            messageData.AClassList[i].ClassInfList.push({
                "Link": message.link,
                "Img": message.img,
                "Time": message.time,
                "Title": message.title,
                "From": message.from,
            });
        });
    }

    return messageData;
}

export var TestData = testData;
export var GetMessage = getMessage;
